//
//  YDStatusPictureView.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/14.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
//itemSize 的宽高
    private let margin:CGFloat = 5
    private let cellWH:CGFloat = 80
//cell的重用标识
private let YDStatusPictureViewCellID = "YDStatusPictureViewCellID"

class YDStatusPictureView: UICollectionView {
//      供外界赋值
    
    var pictureInfos:[YDPictureInfo]?{
        didSet{
        countLable.text = "\(pictureInfos?.count ?? 0)"
//        得到size
            let size = getPictureViewSize(pictureInfos?.count ?? 0)
//            更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
//            刷新 添加的图片
            reloadData()
            
        }
    
    }
//    计算配图的size
    func getPictureViewSize(count:Int) ->CGSize{
       
        let col = count == 4 ? 2 : count > 3 ? 3 :count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
//        计算宽高
        let w = CGFloat (col) * cellWH + CGFloat (col - 1) * margin
        let h = CGFloat (row) * cellWH + CGFloat (row - 1) * margin
        return CGSize(width: w, height: h)
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//  设置视图
    private func setupUI(){
    
        backgroundColor = UIColor.whiteColor()
        addSubview(countLable)
        countLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
//        注册cell
        registerClass(YDStatusPictureViewCell.self, forCellWithReuseIdentifier: YDStatusPictureViewCellID)
//    设置代理
        dataSource = self
        
//        设置layout 否则就会自己调用系统的
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
//        设置itemSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
//        设置垂直和水平间距
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
    
    }
//    懒加载控件
    private lazy var countLable:UILabel = UILabel(textColor: randomColor(), textSize: 40)
}
extension YDStatusPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfos?.count ?? 0
        
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YDStatusPictureViewCellID, forIndexPath: indexPath) as! YDStatusPictureViewCell
        cell.backgroundColor = randomColor()
//        cell赋值
        cell.picUrl = pictureInfos![indexPath.item]
        return cell
    }



}
//创建自定义的collectionViewcell

class YDStatusPictureViewCell:UICollectionViewCell {
    var picUrl:YDPictureInfo?{
        didSet{
        bgimageView.yd_setImageWithString(picUrl?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
//            print(picUrl?.thumbnail_pic)
//            判断图片的后缀是否为gif
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif"){
            gifImageView.hidden = false
            
            }else{
            
            gifImageView.hidden = true
            }
           
        
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    设置视图
    private func setUpUI(){
    backgroundColor = UIColor.whiteColor()
//        添加控件
        contentView.addSubview(bgimageView)
        contentView.addSubview(gifImageView)
        
//        添加约束
        bgimageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.bottom.equalTo(contentView)
        }
    }
//    懒加载控件
//    背景图片
    private lazy var bgimageView:UIImageView = {
    let imageView = UIImageView(imageName: "weibo_placeholder")
//        等比例拉伸图片 防止图片变形
        imageView.contentMode = .ScaleAspectFill
//        裁剪图片
        imageView.clipsToBounds = true
        return imageView
    
    
    }()
//    gif 图片
    private lazy var gifImageView:UIImageView = UIImageView(imageName: "timeline_image_gif")
    
}








